package com.smbms.controller.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.smbms.exception.NoLoginException;

/**
 * 异常拦截
 * @ClassName: MyHandlerExceptionResolver 
 * @Description: (这里用一句话描述这个类的作用) 
 * @author Lyrk.Guo
 * @date 2018年7月14日 下午5:06:08
 */
@ControllerAdvice //注解支持 + 实现HandlerExceptionResolver
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

	
	//业务异常
	
	/**
	 * 未登录异常
	 * @param request
	 * @param arg1
	 * @param arg2
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = NoLoginException.class)
	public String noLogin(HttpServletRequest request, HttpServletResponse arg1, Object arg2,
			NoLoginException e){
		request.setAttribute("error", e.getMessage());	
		return "login";
	}

	//默认异常
	@ExceptionHandler(value = Exception.class)
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse arg1, Object arg2,
			Exception e) {
		//
		e.printStackTrace();
		return new ModelAndView("error");
	}
	
	//没有权限异常
	@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(value = UnauthorizedException.class)
	public String unauthorized(HttpServletRequest request,UnauthorizedException e){
		request.setAttribute("error", e.getMessage());	
		return "unauthorized";
	}

}
